//
//  CircleDetailViewModel.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/3.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "CircleDetailViewModel.h"
#import "CircleDetailReturnTableViewCell.h"
#import "CircleDetailReplyTwoTableViewCell.h"
#import "CirclrDetailHeaderView.h"
#import "CommentBottomView.h"
#import "NoDataFootTableViewHeaderFooterView.h"
#import "CircleDetailArticleTableViewCell.h"

@interface CircleDetailViewModel()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *selfTableView;
    UIView *selfView;
    CirclrDetailHeaderView *headerView;
    CommentBottomView *bottomView;
}

@end

@implementation CircleDetailViewModel

- (instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView
{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {

        tableView.height -= kHeightProportion_In_iPhoneTriploid(30);
        selfTableView = tableView;
        selfTableView.delegate = self;
        selfTableView.dataSource = self;
        selfTableView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        selfTableView.sectionFooterHeight = 0;
        selfTableView = tableView;
        [selfTableView registerClass:[CirclrDetailHeaderView class] forHeaderFooterViewReuseIdentifier:@"CirclrDetailHeaderView"];
        [selfTableView registerClass:[NoDataFootTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"NoDataFootTableViewHeaderFooterView"];
        
        [superView addSubview:tableView];
        selfView = superView;
        
        bottomView = [[CommentBottomView alloc] initWithInSuperView:superView];
        bottomView.BlockShare = ^(GLGWButton * _Nullable shareBtn) {
            DLog(@"分享");
        };
        bottomView.BlockLike = ^(GLGWButton * _Nullable likeBtn) {
            DLog(@"喜欢")
        };
        bottomView.BlockReply = ^(GLGWButton * _Nullable ReplyBtn) {
            DLog(@"回复");
        };
    }
    return self;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return kHeightProportion_In_iPhoneTriploid(10);
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return kHeightProportion_In_iPhoneTriploid(39);
    }
    return kHeightProportion_In_iPhoneTriploid(10);
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    NoDataFootTableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"NoDataFootTableViewHeaderFooterView"];
    return footerView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return 9;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        CircleDetailArticleTableViewCell *cell = [CircleDetailArticleTableViewCell cellWithTableView:tableView];
        [cell refreshPhoto:7];
        return cell;
    }
    BaseTableViewCell *cell;
    if (indexPath.row % 2 == 0) {
        CircleDetailReturnTableViewCell *celll = [CircleDetailReturnTableViewCell cellWithTableView:tableView];
        cell = celll;
        kWeakSelf(self);
        celll.BlockReply = ^(GLGWButton * _Nullable replyBtn) {
            [weakself replyBtn:replyBtn];
        };
        celll.BlockLike = ^(GLGWButton * _Nullable likeBtn) {
            [weakself likeBtn:likeBtn];
        };
    } else {
        CircleDetailReplyTwoTableViewCell *celll = [CircleDetailReplyTwoTableViewCell cellWithTableView:tableView];
        cell = celll;
        kWeakSelf(self);
        celll.BlockReply = ^(GLGWButton * _Nullable replyBtn) {
            [weakself replyBtn:replyBtn];
        };
        celll.BlockLike = ^(GLGWButton * _Nullable likeBtn) {
            [weakself likeBtn:likeBtn];
        };
        celll.BlockAllReply = ^(GLGWButton * _Nullable allReplyBtn) {
            [weakself allReplyBtn:allReplyBtn];
        };
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        CirclrDetailHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CirclrDetailHeaderView"];
        headerView.desStr = @"全部38条评论";
        return headerView;
    }
    return nil;
}


- (void)replyBtn:(GLGWButton *)btn
{
    if (self.BlockReply) {
        self.BlockReply(btn);
    }
}

- (void)likeBtn:(GLGWButton *)btn
{
    if (self.BlockLike) {
        self.BlockLike(btn);
    }
}

- (void)allReplyBtn:(GLGWButton *)btn
{
    if (self.BlockAllReply) {
        self.BlockAllReply(btn);
    }
}

@end
